package com.dfbz.service.impl;

import com.dfbz.dao.UserDao;
import com.dfbz.dao.UserFocusDao;
import com.dfbz.dao.base.UserInfoDao;
import com.dfbz.model.User;
import com.dfbz.model.UserFocus;
import com.dfbz.model.UserInfo;
import com.dfbz.service.UserServices;
import com.dfbz.utils.MailUtil;
import com.dfbz.utils.StrUtil;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserServices {

    private UserDao userDao = new UserDao();

    private UserInfoDao userInfoDao = new UserInfoDao();

    private UserFocusDao userFocusDao = new UserFocusDao();

    @Override
    public User findByName(String username) {
        List<User> userList = userDao.findByCol("username", username);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public User findByEmail(String email) {
        List<User> userList = userDao.findByCol("email", email);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public boolean checkByEmailOrName(String email, String username) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("email", email);
        List<User> userList = userDao.findByCols("or", map);
        return userList.isEmpty();
    }

    @Override
    public boolean addUser(User user) {
        if (userDao.save(user) > 0) {
            Map<String, Object> map = userDao.getBaseInfoByName(user.getUsername());
            // 还需要添加用户info表信息
            UserInfo info = new UserInfo();
            info.setUserId(Integer.parseInt(map.get("user_id").toString()));
            info.setRealName("Name");
            info.setRegisterTime(new Date());
            return userInfoDao.save(info) > 0;
        }
        return false;
    }

    @Override
    public String sendEmailCode(String email) {
        String code = StrUtil.getRandomCode(4);
        // 用一个工具类，实现真的发送验证码给用户
        String subject = "《小标交友》重制密码";
        String content = "您的验证码是：" + code + "十分钟之内有效";
        try {
            MailUtil.send(email, subject, content, MailUtil.UNICODE);
        } catch (MessagingException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return code;
    }

    @Override
    public boolean resetPassword(String email, String password) {
        List<User> users = userDao.findByCol("email", email);
        if (!users.isEmpty()) {
            User user = users.get(0);
            user.setPassword(password);
            return userDao.update(user) > 0;
        }
        return false;
    }

    @Override
    public Map<String, Object> findUserBaseInfo(String username) {
        return userDao.getBaseInfoByName(username);
    }

    @Override
    public Map<String, Object> getUserInfo(String userId) {
        return userDao.getUserInfoById(userId);
    }

    @Override
    public List<Map<String, Object>> getUserList(String userId, int currentPage, String name) {
        return userDao.findUsers(userId, currentPage, name);
    }

    @Override
    public long getUserCount(String name) {
        return userDao.getUserTotal(name);
    }

    @Override
    public void updateLoginTime(Integer userId) {
        UserInfo info = userInfoDao.findById(userId);
        if (null != info) {
            info.setLoginTime(new Date());
            userInfoDao.update(info);
        }
    }

    @Override
    public boolean followUser(String userId, String ufId) {
        UserFocus focus = new UserFocus();
        focus.setUserId(Integer.parseInt(userId));
        focus.setUserFocusId(Integer.parseInt(ufId));
        return userFocusDao.save(focus) > 0;
    }

    @Override
    public UserFocus getFollowUser(String userId, String ufId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("user_focus_id",ufId);
        List<UserFocus> focusList = userFocusDao.findByCols("and", map);
        if (!focusList.isEmpty()){
            return focusList.get(0);
        }
        return null;
    }

    @Override
    public boolean unFollowUser(Integer id) {
        return userFocusDao.deleteById(id) > 0;
    }


}
